﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;

namespace Card.Manager
{
    public partial class TemplateManager : BasePage
    {
        #region
        private int Records
        {
            get
            {
                int _Records = 50;
                if (int.TryParse(this.txtRecords.Text, out _Records) && _Records > 0)
                {
                    return _Records;
                }
                else
                {
                    this.txtRecords.Text = "50";
                    return 50;
                }
            }
        }
        #endregion

        #region 事件
        protected override void OnInit(EventArgs e)
        {
            this.btnQuery.Click += new EventHandler(btnQuery_Click);
            this.rptList.ItemCommand += new RepeaterCommandEventHandler(rptList_ItemCommand);
            //this.pager.BuildingUrl += new BAF.WebControls.BuildingUrlHander(pager_BuildingUrl);
            this.page.PageIndexChange += new Card.Manager.Controls.PageIndexChangeHandler(page_PageIndexChange);
            base.OnInit(e);
        }

        void page_PageIndexChange(object sender, Card.Manager.Controls.PageIndexChangeEventArgs e)
        {
            this.DoQuery();
        }

        string pager_BuildingUrl(int index)
        {
            return string.Format("TemplateManager.aspx?i={0}", index);
        }

        void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (string.Compare(e.CommandName, "DELETE") == 0)
            {
                int id = 0;
                if (int.TryParse(e.CommandArgument.ToString(), out id))
                {
                    BLL.TemplateManager.Delete(id, this.CPassport);
                    BLL.TemplateFieldManager.DeleteByTemplateID(id, this.CPassport);
                    BLL.TemplateLogoManager.Delete(id, this.CPassport);
                    this.DoQuery();
                    this.MessageBox("删除成功！");
                }                
            }
        }

        void btnQuery_Click(object sender, EventArgs e)
        {
            this.DoQuery();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.DoInit();
            }
        }
        #endregion

        #region 方法
        private void DoInit()
        {
            List<BAF.Entity.LookupValue> lstColor = Card.Common.LookUpCache.GetLookupValues(CONST.LOOKUP_TYPE.TEMPLATE_COLOR);
            List<BAF.Entity.LookupValue> lstTemplateType = Card.Common.LookUpCache.GetLookupValues(CONST.LOOKUP_TYPE.TEMPLATE_TYPE);
            Card.Utility.ControlUtility.BindListControl<BAF.Entity.LookupValue>(this.dropColor, lstColor, "Description", "LookupCode",true);
            Card.Utility.ControlUtility.BindListControl<BAF.Entity.LookupValue>(this.dropTemplateType, lstTemplateType, "Value", "LookupCode", true);
            this.DoQuery();
        }

        private void DoQuery()
        {
            //this.pager.PageSize = this.Records;
            Card.DataBase.Pagination pagination = new Card.DataBase.Pagination()
            {
                TableName = "TEMPLATE",
                PageSize = this.page.PageSize,
                OrderBy = "ID",
                PageNumber = this.page.PageIndex + 1,
                Condition = GetCondition()
            };

            DataTable dtData = BLL.TemplateManager.Query(pagination);
            this.page.RecordCount = pagination.RecordCount;
            this.rptList.DataSource = dtData;
            this.rptList.DataBind();
        }

        private string GetCondition()
        {
            StringBuilder sb = new StringBuilder(" AND (NAME LIKE '%" + this.txtName.Text + "%' OR CONVERT(NVARCHAR,ID)  LIKE  '%" + this.txtName.Text + "%') ");
            if (!string.IsNullOrEmpty(this.dropColor.SelectedValue))
            {
                sb.Append(" AND COLOR='").Append(this.dropColor.SelectedValue).Append("'");
            }

            if (!string.IsNullOrEmpty(this.dropTemplateType.SelectedValue))
            {
                sb.Append(" AND TYPE_CODE='").Append(this.dropTemplateType.SelectedValue).Append("'");
            }

            if (!string.IsNullOrEmpty(this.dropStyle.SelectedValue))
            {
                sb.Append(" AND STYLE='").Append(this.dropStyle.SelectedValue).Append("'");
            }
            if (!string.IsNullOrEmpty(this.dropStatus.SelectedValue))
            {
                sb.Append(" AND STATUS='").Append(this.dropStatus.SelectedValue).Append("'");
            }
            return sb.ToString();
        }
        /// <summary>
        /// 模版状态
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public string GetStatusDesc(string code)
        {
            switch (code)
            {
                case CONST.TEMPLATE_STATUS.BUILDING:
                    return "建设中";
                case CONST.TEMPLATE_STATUS.ACTIVE:
                    return "启用";
                case CONST.TEMPLATE_STATUS.INACTIVE:
                    return "禁用";
                default:
                    break;
            }
            return string.Empty;
        }
        #endregion
    }
}
